<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="roleDialogFormRef" :model="state" :rules="state.rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色名称" prop="ruleForm.roleName">
							<el-input v-model="state.ruleForm.roleName" placeholder="请输入角色名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色标识" prop="ruleForm.roleCode">
							<el-input v-model="state.ruleForm.roleCode" placeholder="请输入角色标识" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色排序" prop="ruleForm.roleSort">
							<el-input v-model="state.ruleForm.roleSort" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="是否启用"  prop="ruleForm.menuStatus" >
								<el-radio-group v-model="state.ruleForm.roleStatus">
									<el-radio label="1">启用</el-radio>
									<el-radio label="0">不启用</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="角色描述" >
							<el-input v-model="state.ruleForm.roleDescribe" type="textarea" placeholder="请输入角色描述" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="菜单权限">
							<el-tree ref="treeRef" :data="state.menuData" :props="state.menuProps" node-key="id" :default-checked-keys="state.ruleForm.menuList" default-expand-all show-checkbox class="menu-data-tree" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(roleDialogFormRef)" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemRoleDialog">
import { reactive, ref, onMounted } from 'vue';
import type { FormInstance } from 'element-plus'
import { ElMessage,ElTree  } from 'element-plus';
import { roleApi } from './api';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 引入 api 请求接口
const api = roleApi();
const treeRef = ref<InstanceType<typeof ElTree>>();

// 定义变量内容
const roleDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id:'',//主键id
		roleName: '', // 角色名称
		roleCode: '', // 角色标识
		roleSort: 0, // 排序
		roleStatus: "1", // 角色状态
		roleDescribe: '', // 角色描述
		menuList: [] as any[],//菜单权限集合
	},
	menuData: [] as any[],
	
	menuProps: {
		children: 'children',
		label: 'menuName',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	rules: {
		'ruleForm.roleName': { required: true, message: '请输入角色名称', trigger: 'blur' },
		'ruleForm.roleSort': { required: true, message: '请输入角色排序', trigger: 'blur' },
		'ruleForm.roleStatus': { required: true, message: '请选择是否启用', trigger: 'blur' },
	},
});

// 打开弹窗
const openDialog = (type: string, row: any) => {
	
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改角色';
		state.dialog.submitTxt = '修 改';
		state.dialog.isShowDialog = true;
		//从后台获取该菜单的所有上级菜单，用于回显
		api.getRoleAndMenu({'id':row.id}).then(
			function (res) {
				// state.ruleForm.menuList =  res.data;
				treeRef.value!.setCheckedKeys([], false)
				treeRef.value!.setCheckedKeys(res.data, false)
			}
		)
	} else {
		//新增时 主键id
		state.ruleForm.id = '';
		state.dialog.title = '新增角色';
		state.dialog.submitTxt = '新 增';
		state.dialog.isShowDialog = true;
	}
	state.dialog.type = type;
	
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = (formEl: FormInstance | undefined) => {
	console.log(treeRef.value!.getCheckedKeys(false))
	state.ruleForm.menuList = treeRef.value!.getCheckedKeys(false);

	if (!formEl) return
	formEl.validate((valid) => {
		if (valid) {
			// console.log('校验通过！')
			api.save(state.ruleForm).then(
				(res) => {
					if (res.data.code === "000000") {
						ElMessage.success(res.data.message);
						closeDialog(); // 关闭弹窗
						emit('refresh');
					} else {
						ElMessage.warning(res.data.message);
					}
				}
			)
		} else {
			// console.log('校验未通过！')
			return false
		}
	})
};

// 获取所有菜单
const getAllMenu = () => {
	api.getMenuList().then(
		function (res) {
			state.menuData = res.data;
		}
	)
};

// 页面加载时
onMounted(() => {
	getAllMenu();
});

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
